<?php
/**
 * @title 844. 比较含退格的字符串 - 双指针+倒序比较
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $S
     * @param String $T
     * @return Boolean
     */
    function backspaceCompare($S, $T) {
        /**
         * @since 2020-10-19 指针
         */
        $sPoint = strlen($S)-1;
        $tPoint = strlen($T)-1;

        /**
         * @since 2020-10-19 倒序比较
         */
        while($sPoint >=0 AND $tPoint>=0){
            /**
             * @since 2020-10-19 处理退格符, 找到非空格字符
             */
            $sPoint = self::find($S, $sPoint);
            $tPoint = self::find($T, $tPoint);

            /**
             * @since 2020-10-19 同时到达头部
             */
            if($sPoint<0 AND $tPoint<0){
                return true;
            } elseif($sPoint<0 OR $tPoint<0) {
                /**
                 * @since 2020-10-19 一个提前到达头部
                 */
                return false;
            } else {
                /**
                 * @since 2020-10-19 比较
                 */
                // echo "S[{$sPoint}]=", $S[$sPoint], ", T[{$tPoint}]=", $T[$tPoint], PHP_EOL;
                if($S[$sPoint] != $T[$tPoint]){
                    return false;
                    break;
                } else {
                    $sPoint--;
                    $tPoint--;
                }
            }
        }

        /**
         * @since 2020-10-19 收官
         */
        if($sPoint >= 0){
            $sPoint = self::find($S, $sPoint);
        } else {}
        if($tPoint >= 0){
            $tPoint = self::find($T, $tPoint);
        } else {}

        /**
         * @return
         */
        return ($sPoint<0 AND $tPoint<0);
    }

    /**
     * @title 找到非退格字符
     * @author start2004
     * @since 2020-10-19 Created
     *
     * @param string $S
     * @param int $sPoint
     * @return int
     */
    public function find($S = '', $sPoint = 0){
        $backNum = 0;

        /**
         * @since 2020-10-19 找到非退格字符
         */
        while($S[$sPoint] == "#" OR $backNum>0){
            // echo "___s[{$sPoint}]=", $S[$sPoint], ", backNum=", $backNum, PHP_EOL;
            if($S[$sPoint] == "#"){
                $backNum++;
            } else {
                $backNum--;
            }
            $sPoint--;
            if($sPoint<0){
                break;
            } else {}
        }

        /**
         * @return
         */
        return $sPoint;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.19_2.php
 */
$datas = [
    // ["ab#c", "ad#c",],
    // ["ab##", "c#d#",],
    // ["a##c", "#a#c"],
    // ["a#c", "b"],
    ["nzp#o#g", "b#nzp#o#g"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->backspaceCompare(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
